package cn.yunhe.insurance.service.impl;


import cn.yunhe.insurance.dao.InsurCateDao;
import cn.yunhe.insurance.entity.Insurcate;
import cn.yunhe.insurance.service.InsurcateService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author 董稳钰
 * @date 2018/9/17 19:27
 */
@Service("insurcateService")
public class InsurcateServiceImpl implements InsurcateService {

    @Resource
    private InsurCateDao insurCateDao;

    @Override
    @Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT,timeout=36000,rollbackFor=Exception.class)
    public boolean addInsurcate(Insurcate insurcate) {
        return insurCateDao.addInsurcate(insurcate);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT,timeout=36000,rollbackFor=Exception.class)
    public boolean updateInsurcate(Insurcate insurcate) {
        return insurCateDao.updateInsurcate(insurcate);
    }

    @Override
    public Page<Insurcate> findLikeInsurcates(Map<String, Object> cond) {
        Page<Insurcate> page=new Page<Insurcate>();

        page.setTotal(insurCateDao.findLikeInsurcatesCount(cond));
        //获取总记录数
        int currPageNum=Integer.parseInt(String.valueOf(cond.get("pageNum")));
        //获取当前页
        int pageSize=Integer.parseInt(String.valueOf(cond.get("pageSize")));
        //获取每页的大小
        page.setPageSize(pageSize);

        page.setPageNum((int) (page.getTotal()/pageSize+(page.getTotal()%pageSize==0?0:1)));
        //计算出总页数
        page=PageHelper.startPage(currPageNum,pageSize);
        List<Insurcate> list=insurCateDao.findLikeInsurcates(cond);
        //查询保险
        return page;

    }
    @Override
    public List<Insurcate> getInsurcateByComuuid(String comuuid) {
        return insurCateDao.getInsurcateByComuuid(comuuid);
    }


    @Override
    public int findLikeInsurcatesCount(Map<String, Object> cond) {
        return 0;
    }

    @Override
    public Insurcate getInsurcateById(Insurcate insurcate) {
        return insurCateDao.getInsurcateById(insurcate);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT,timeout=36000,rollbackFor=Exception.class)
    public boolean delInsurcate(Insurcate insurcate) {
        return insurCateDao.delInsurcate(insurcate);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT,timeout=36000,rollbackFor=Exception.class)
    public boolean delInsurcates(List<String>iuuidList) {
        return insurCateDao.delInsurcates(iuuidList);
    }

    @Override
    public boolean adoptInsurcate(String iuuid) {
        return insurCateDao.adoptInsurcate(iuuid);
    }

    @Override
    public boolean refuseInsurcate(String iuuid) {
        return insurCateDao.refuseInsurcate(iuuid);
    }

    @Override
    public List<Insurcate> getAllInsurcate() {
        return  null;
    }

    @Override
    public List<Insurcate> getAllUseableInsurcate() {
        return insurCateDao.getAllUseableInsurcate();
    }

    public InsurCateDao getInsurCateDao() {
        return insurCateDao;
    }

    public void setInsurCateDao(InsurCateDao insurCateDao) {
        this.insurCateDao = insurCateDao;
    }
}
